package com.seeTools.next.common.tools;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.seeTools.next.common.constant.Constants;
import com.seeTools.next.common.core.ServiceException;
import com.seeTools.next.common.tools.redis.RedisUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @Author: seeTools
 * @Date: 2024/5/9  8:33
 * @Version 1.0
 * 订单工具
 */
@Slf4j
public class OrderUtils {

    private static final RedisUtil redisUtil = SpringUtils.getBean(RedisUtil.class);

    /**
     * 积分订单号
     * 会员ID：时间：随机数
     *
     * @return
     */
    public static String pointNo(Long memberId) {
        String date = DateUtil.format(DateUtil.date(), DatePattern.PURE_DATE_PATTERN);
        return memberId + ":" + date + ":" + IdUtil.fastSimpleUUID();
    }

    /**
     * 生成12位卡号
     *
     * @return
     */
    public static String getCardNo() {
        long num = 12;
        long incr = redisUtil.incr(Constants.CacheKey.CACHE_CARD_MASTER_INDEX, 1);
        if (ObjectUtil.isNotEmpty(incr)) {
            String head = "02";
            int createNum = (int) (num - StrUtil.toString(incr).length());
            String str = StrUtil.padAfter(head, createNum, "0");
            return str + incr;
        }
        log.error("找不到储值卡卡号缓存，请检查缓存数据");
        throw new ServiceException("找不到储值卡卡号缓存，请检查缓存数据");
    }
}
